//
//  LaunchViewController.m
//  SeeRice
//
//  Created by 徐州外服 on 17/1/24.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "LaunchViewController.h"
#define WIDTH (NSInteger)self.view.bounds.size.width
#define HEIGHT (NSInteger)self.view.bounds.size.height
@interface LaunchViewController ()<UIScrollViewDelegate>
{
    // 创建页码控制器
    UIPageControl *pageControl;
    // 判断是否是第一次进入应用
    BOOL flag;
}

@property (nonatomic ,strong) NSMutableArray *dataSource;

@end

@implementation LaunchViewController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestDataWelcome];
    
}



#pragma mark - 请求欢迎页接口
- (void)requestDataWelcome {
    
    //http://www.4008000114.com:8099/HomePage/Welcome.ashx
    NSString *urlStr = @"HomePage/Welcome.ashx";
    [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:nil success:^(id responseObject) {
//        NSLog(@"%@",responseObject);
        if (SUCCESS) {
            NSDictionary *data = [responseObject objectForKey:@"DATA"];
            self.dataSource = data[@"IMAGES"];
            
            [self setup];
        }else {
            
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

- (void)setup {
    
    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    for (int i=0; i<self.dataSource.count; i++) {
        //        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"引导页0%d",i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        NSString *urlStr = [NSString stringWithFormat:@"%@",self.dataSource[i]];
        [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@""]];
        
        //                if (i == 0 || i == 1) {
        // 必须设置用户交互 否则按键无法操作
        imageView.userInteractionEnabled = YES;
        //跳过
        UIButton *skipnBtn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
        [skipnBtn setFrame:(CGRectMake(kScreenWidth - 50, 20, 40, 40))];
        [skipnBtn setTitle:@"跳过" forState:(UIControlStateNormal)];
        skipnBtn.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.2];
        [skipnBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [skipnBtn.layer setMasksToBounds:YES];
        [skipnBtn.layer setCornerRadius:skipnBtn.frame.size.width/2]; //设置矩圆角半径
        skipnBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [skipnBtn addTarget:self action:@selector(go:) forControlEvents:(UIControlEventTouchUpInside)];
        [imageView addSubview:skipnBtn];
        
        //                }
        
        
        // 在最后一页创建按钮
        if (i == 5) {
            // 必须设置用户交互 否则按键无法操作
            imageView.userInteractionEnabled = YES;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(WIDTH / 3, HEIGHT * 7 / 8, WIDTH / 3, HEIGHT / 16);
            button.backgroundColor = [UIColor darkGrayColor];
            [button setTitle:@"点击进入" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button.layer setMasksToBounds:YES];
            [button.layer setCornerRadius:8.f];
            [button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
        }
        //        imageView.image = image;
        [myScrollView addSubview:imageView];
    }
    myScrollView.bounces = NO;
    myScrollView.pagingEnabled = YES;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.contentSize = CGSizeMake(WIDTH * self.dataSource.count, HEIGHT);
    myScrollView.delegate = self;
    [self.view addSubview:myScrollView];
    
    
    
    pageControl = [[UIPageControl alloc] init];
    pageControl.frame = CGRectMake(WIDTH / 3, HEIGHT *15 / 16, WIDTH / 3, HEIGHT / 16);
    //有图片显示图片、没图片则显示设置颜色
    pageControl.pageIndicatorTintColor =[UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    //    // 设置当前页数显示的图片
    //    pageControl.currentImage = [UIImage imageNamed:@"detail_tupianlunbo_suiji"];
    //    // 设置当前默认页数显示的图片
    //    pageControl.defaultImage = [UIImage imageNamed:@"detail_piclunbounselec_suiji"];
    //    // 设置图片的大小
    //    pageControl.pageSize = CGSizeMake(12, 12);
    // 设置页数
    pageControl.numberOfPages = self.dataSource.count;
    // 设置默认页数
    pageControl.currentPage = 0;
    
    [self.view addSubview:pageControl];

}


#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 计算当前在第几页
    pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
}
// 点击按钮保存数据并切换根视图控制器
- (void) go:(UIButton *)sender{
    flag = YES;
    
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    // 保存用户数据
    [useDef setBool:flag forKey:@"notFirst"];
    [useDef synchronize];
    // 切换根视图控制器
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIStoryboard *storyBord = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tabBarController = [storyBord instantiateViewControllerWithIdentifier:@"RootViewController"];
    keyWindow.rootViewController = tabBarController;
    
}


@end
